package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NCementStationDataBo;
import com.ruoyi.zhgd.domain.vo.NCementStationDataVo;
import com.ruoyi.zhgd.domain.NCementStationData;
import com.ruoyi.zhgd.mapper.NCementStationDataMapper;
import com.ruoyi.zhgd.service.INCementStationDataService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import com.baomidou.dynamic.datasource.annotation.DS;

/**
 * 水稳拌合站数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NCementStationDataServiceImpl implements INCementStationDataService {

    private final NCementStationDataMapper baseMapper;

    /**
     * 查询水稳拌合站数据
     */
    @Override
    public NCementStationDataVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询水稳拌合站数据列表
     */
    @Override
    public TableDataInfo<NCementStationDataVo> queryPageList(NCementStationDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NCementStationData> lqw = buildQueryWrapper(bo);
        Page<NCementStationDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水稳拌合站数据列表
     */
    @Override
    public List<NCementStationDataVo> queryList(NCementStationDataBo bo) {
        LambdaQueryWrapper<NCementStationData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NCementStationData> buildQueryWrapper(NCementStationDataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NCementStationData> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOrganizationId()), NCementStationData::getOrganizationId, bo.getOrganizationId());
        lqw.eq(StringUtils.isNotBlank(bo.getStationId()), NCementStationData::getStationId, bo.getStationId());
        lqw.eq(StringUtils.isNotBlank(bo.getMatchId()), NCementStationData::getMatchId, bo.getMatchId());
        lqw.eq(bo.getDischargeTime() != null, NCementStationData::getDischargeTime, bo.getDischargeTime());
        lqw.eq(bo.getBatchWeight() != null, NCementStationData::getBatchWeight, bo.getBatchWeight());
        lqw.eq(bo.getGravel1Act() != null, NCementStationData::getGravel1Act, bo.getGravel1Act());
        lqw.eq(bo.getGravel1RatioTheo() != null, NCementStationData::getGravel1RatioTheo, bo.getGravel1RatioTheo());
        lqw.eq(bo.getGravel1RatioAct() != null, NCementStationData::getGravel1RatioAct, bo.getGravel1RatioAct());
        lqw.eq(bo.getGravel2Act() != null, NCementStationData::getGravel2Act, bo.getGravel2Act());
        lqw.eq(bo.getGravel2RatioTheo() != null, NCementStationData::getGravel2RatioTheo, bo.getGravel2RatioTheo());
        lqw.eq(bo.getGravel2RatioAct() != null, NCementStationData::getGravel2RatioAct, bo.getGravel2RatioAct());
        lqw.eq(bo.getGravel3Act() != null, NCementStationData::getGravel3Act, bo.getGravel3Act());
        lqw.eq(bo.getGravel3RatioTheo() != null, NCementStationData::getGravel3RatioTheo, bo.getGravel3RatioTheo());
        lqw.eq(bo.getGravel3RatioAct() != null, NCementStationData::getGravel3RatioAct, bo.getGravel3RatioAct());
        lqw.eq(bo.getGravel4Act() != null, NCementStationData::getGravel4Act, bo.getGravel4Act());
        lqw.eq(bo.getGravel4RatioTheo() != null, NCementStationData::getGravel4RatioTheo, bo.getGravel4RatioTheo());
        lqw.eq(bo.getGravel4RatioAct() != null, NCementStationData::getGravel4RatioAct, bo.getGravel4RatioAct());
        lqw.eq(bo.getSand1Act() != null, NCementStationData::getSand1Act, bo.getSand1Act());
        lqw.eq(bo.getSand1RatioTheo() != null, NCementStationData::getSand1RatioTheo, bo.getSand1RatioTheo());
        lqw.eq(bo.getSand1RatioAct() != null, NCementStationData::getSand1RatioAct, bo.getSand1RatioAct());
        lqw.eq(bo.getSand2Act() != null, NCementStationData::getSand2Act, bo.getSand2Act());
        lqw.eq(bo.getSand2RatioTheo() != null, NCementStationData::getSand2RatioTheo, bo.getSand2RatioTheo());
        lqw.eq(bo.getSand2RatioAct() != null, NCementStationData::getSand2RatioAct, bo.getSand2RatioAct());
        lqw.eq(bo.getCement1Act() != null, NCementStationData::getCement1Act, bo.getCement1Act());
        lqw.eq(bo.getCement1RatioTheo() != null, NCementStationData::getCement1RatioTheo, bo.getCement1RatioTheo());
        lqw.eq(bo.getCement1RatioAct() != null, NCementStationData::getCement1RatioAct, bo.getCement1RatioAct());
        lqw.eq(bo.getCement2Act() != null, NCementStationData::getCement2Act, bo.getCement2Act());
        lqw.eq(bo.getCement2RatioTheo() != null, NCementStationData::getCement2RatioTheo, bo.getCement2RatioTheo());
        lqw.eq(bo.getCement2RatioAct() != null, NCementStationData::getCement2RatioAct, bo.getCement2RatioAct());
        lqw.eq(bo.getMaterial9Act() != null, NCementStationData::getMaterial9Act, bo.getMaterial9Act());
        lqw.eq(bo.getMaterial9RatioTheo() != null, NCementStationData::getMaterial9RatioTheo, bo.getMaterial9RatioTheo());
        lqw.eq(bo.getMaterial9RationAct() != null, NCementStationData::getMaterial9RationAct, bo.getMaterial9RationAct());
        lqw.eq(bo.getInsertTime() != null, NCementStationData::getInsertTime, bo.getInsertTime());
        return lqw;
    }

    /**
     * 新增水稳拌合站数据
     */
    @Override
    public Boolean insertByBo(NCementStationDataBo bo) {
        NCementStationData add = BeanUtil.toBean(bo, NCementStationData.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改水稳拌合站数据
     */
    @Override
    public Boolean updateByBo(NCementStationDataBo bo) {
        NCementStationData update = BeanUtil.toBean(bo, NCementStationData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NCementStationData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水稳拌合站数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
